package io.renren.modules.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.member.dao.ExamMiddleHistoryDao;
import io.renren.modules.member.dto.ExamDto;
import io.renren.modules.member.entity.ExamMiddleHistoryEntity;
import io.renren.modules.member.entity.MemberEntity;
import io.renren.modules.member.service.ExamMiddleHistoryService;
import io.renren.modules.member.service.MemberService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service("examMiddleHistoryService")
public class ExamMiddleHistoryServiceImpl extends ServiceImpl<ExamMiddleHistoryDao, ExamMiddleHistoryEntity> implements ExamMiddleHistoryService {

    private final Logger logger = LoggerFactory.getLogger(ExamMiddleHistoryServiceImpl.class);

    @Autowired
    private MemberService memberService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String examDate = (String) params.get("examDate");
        String studentCode = (String) params.get("studentCode");
        String name = (String) params.get("name");

        IPage<ExamMiddleHistoryEntity> page = this.page(
                new Query<ExamMiddleHistoryEntity>().getPage(params),
                new QueryWrapper<ExamMiddleHistoryEntity>()
                        .eq(StringUtils.isNotBlank(studentCode), "student_code", studentCode)
                        .eq(StringUtils.isNotBlank(examDate), "exam_date", examDate)
                        .like(StringUtils.isNotBlank(name), "name", name)
        );
        return new PageUtils(page);
    }

    public List<ExamMiddleHistoryEntity> findAllLast(Map<String, Object> params) {
        String showNumber = (String) params.get("showNumber");
        if (Objects.isNull(showNumber)) {
            params.put("showNumber", 5);
        } else {
            try {
                params.put("showNumber", Integer.parseInt(showNumber));
            } catch (Exception e) {
                params.put("showNumber", 5);
            }
        }
        return baseMapper.findAllLast(params);
    }

    public void save(ExamDto dto, BigDecimal middleRunScore) {

        if (Optional.ofNullable(dto.getTotalCircle()).orElse(0) < Optional.ofNullable(dto.getCircle()).orElse(0)) {
            logger.info("数据跑圈大于总圈数有误：{}", dto.toString());
            return;
        }
        ExamMiddleHistoryEntity entity = new ExamMiddleHistoryEntity();
        BeanUtils.copyProperties(dto, entity);
        int minute = dto.getAchieve().intValue() / 1000 / 60;
        int second = (dto.getAchieve().intValue() / 1000) % 60;
        entity.setMiddleRun(new BigDecimal(minute + "." + second));
        entity.setMiddleRunScore(middleRunScore);
        entity.setExamDate(dto.getDate());
        entity.setCreateDate(new Date());
        MemberEntity memberEntity =  memberService.findByStudentCode(dto.getStudentCode());
        if (Objects.nonNull(memberEntity)) {
            entity.setName(memberEntity.getName());
            baseMapper.insert(entity);
        }
    }

}
